package edu.guet.server.service;

import edu.guet.server.mapper.UnitTestMapper;
import edu.guet.server.model.UnitTest;
import edu.guet.server.model.dto.StuUnitTest;
import edu.guet.server.utils.ResultAPI;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UnitTestService {
    @Autowired
    UnitTestMapper unitTestMapper;

    //有点问题暂时不用
    public ResultAPI setUnitTest(UnitTest unitTest) {
        String start = unitTest.getStart();
        String end = unitTest.getEnd();

        Date start_time = null;
        Date end_time = null;

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            start_time = df.parse(start);
            end_time = df.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        unitTest.setStart_time(start_time);
        unitTest.setEnd_time(end_time);

        if (start_time.before(end_time)) {
            unitTest.setStatus((byte) 1);
        } else if (start_time.after(end_time)) {
            unitTest.setStatus((byte) 0);
        } else {
            unitTest.setStatus((byte) 0);
        }

        unitTestMapper.setUnitTest(unitTest);
        return ResultAPI.success(unitTest, "成功设置单元题目");
    }
    public ResultAPI InsertTest(StuUnitTest unitTest) {
        boolean flag = unitTestMapper.insertUnitTest(unitTest);
        if (flag) {
            return ResultAPI.success(unitTest);
        } else {
            return ResultAPI.error("添加失败");
        }
    }
    public List<StuUnitTest> findTests(Integer unitId) {
        return unitTestMapper.findTests(unitId);
    }

    public void updateTestStatus(Integer unitTestId, Integer status) {
        unitTestMapper.updateTestStatus(unitTestId,status);
    }

    public ResultAPI getUnitTest() {
        return ResultAPI.success(unitTestMapper.getUnitTest(), "成功获取所有单元题目");
    }

    public ResultAPI getUnitTestById(int unit_id) {
        return ResultAPI.success(unitTestMapper.getUnitTestById(unit_id), "成功获取该单元题目");
    }

    public ResultAPI deleteUnitTest(int unit_test_id) {
        boolean flag = unitTestMapper.deleteUnitTest(unit_test_id);

        if (flag) {
            return ResultAPI.success("成功删除该单元题目");
        } else {
            return ResultAPI.error("删除失败");
        }
    }

    public ResultAPI updateTest(StuUnitTest unitTest) {
        if(unitTest.getPaper_id()!=null){
            unitTestMapper.insertTestPaper(unitTest);
        }
        boolean flag = unitTestMapper.updateTest(unitTest);

        if (flag) {
            return ResultAPI.success();
        } else {
            return ResultAPI.error("更新失败");
        }
    }

    public ResultAPI getTechTest(Integer unitTestId) {
        StuUnitTest stuUnitTest=unitTestMapper.getTechTest(unitTestId);
        return ResultAPI.success(stuUnitTest);
    }
}
